package com.efounder.fragment;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.efounder.adapter.CommentPagerAdapter;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.mobilemall.R;

/**
 * Created by will on 18-2-24.
 */

public class MallOrderCommentFragment extends BaseFragment {

    private View view;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String[] titles;

    public MallOrderCommentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.layout_frag_order_comment, container, false);

        titles = getResources().getStringArray(R.array.comment);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);

        initViewPager();
        initTabLayout();
        return view;
    }

    private void initViewPager() {
        viewPager.setAdapter(new CommentPagerAdapter(getChildFragmentManager(), titles));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initTabLayout() {
        for (int i=0;i<titles.length;i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).select();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    ((TextView)tab.getCustomView()).setTextColor(getResources().getColor(R.color.black));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    ((TextView)tab.getCustomView()).setTextColor(getResources().getColor(R.color.gray));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

  /*  private View rootView;
    private List<String> images = new ArrayList<>();
    private Fragment f1;
    private  Fragment f2;

    public MallOrderCommentFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_frag_order_comment, container, false);
        RadioGroup radioGroup = view.findViewById(R.id.radio);
//        initView(view);

        //图文详情布局的点击事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.btn1) {
                    initFragment1();
                } else if (checkedId == R.id.btn2) {
                    initFragment2();
                }
            }
        });

        initFragment1();

        return view;
    }

    //显示第一个fragment
    private void initFragment1() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        if (f1 == null) {
            f1 = AssessFragment.newInstance("1");
        }
        transaction.replace(R.id.frame, f1);
        transaction.commit();
    }

    private void initFragment2() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (f2 == null) {
            f2 = AssessFragment.newInstance("2");
        }
        transaction.replace(R.id.frame, f2);
        transaction.commit();
    }

    private void initView(View view) {
        images.add("http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png");
        images.add("http://pic9.nipic.com/20100916/2531170_131413079748_2.jpg");
        Banner banner = (Banner) view.findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置轮播时间
        banner.setDelayTime(3000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }*/

}
